package com.jvbc.xoso.db;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.jvbc.xoso.bean.NewsPage;

public class NewsPageDataSource {
	private SQLiteDatabase db;
	private DataBaseHelper helper;
	
	
	public NewsPageDataSource(Context context){
		helper = new DataBaseHelper(context);
	}
	
	// Them  moi
	public void addNewsPage(NewsPage newspage) {
		db = helper.getWritableDatabase();	
		ContentValues values = new ContentValues();  
		values.put(DataBaseHelper.KEY_NAME, newspage.getName());  
		
		// Inserting Row
		db.insert(DataBaseHelper.TABLE_NEWSPAGE, null, values);
		
		db.close(); // Closing database connection
	}
	// Getting All newspage
	public List<NewsPage> getAllNewsPage() {
		List<NewsPage> listnews = new ArrayList<NewsPage>();
		// Select All Query
		String selectQuery = "SELECT * FROM newspage";
					
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				NewsPage newspage = new NewsPage();
				newspage.setId(Integer.parseInt(cursor.getString(0)));
				newspage.setName(cursor.getString(1));
				// Adding taxi to list
				listnews.add(newspage);
			} while (cursor.moveToNext());
		}
		// close inserting data from database
		db.close();
		// return taxi list
		return listnews;
			
	}
}
